#include <windows.h>

#include "resource.h" 
#include "proiect.h" 

NODE* root[10];
int nr_root=0;
int curr_heap;

BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
				
	switch(Message)
	{

		case WM_INITDIALOG:
			// This is where we set up the dialog box, and initialise any default values

			SetDlgItemText(hwnd, IDC_TEXT, "Insert a value");
			
			root[nr_root] = CREATE_BHEAP();
				SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
					curr_heap=nr_root;
					nr_root++;
					SetDlgItemInt(hwnd, IDC_SHOWCOUNT2 , curr_heap,FALSE);
					SetDlgItemInt(hwnd, IDC_SHOWCOUNT , nr_root,FALSE);
		//	SetDlgItemInt(hwnd, IDC_SHOWCOUNT , 0,FALSE);
			
		break;
		
		case WM_COMMAND:
			
			switch(LOWORD(wParam))
			{
				
				case IDC_ADD:
				{

					BOOL bSuccess;
					int value = GetDlgItemInt(hwnd, IDC_TEXT, &bSuccess, FALSE);
					if(bSuccess) 
					{
						NODE* temp = CREATE_BNODE(value);
						root[curr_heap] = INSERT_BHEAP(root[curr_heap], temp);

						char**buf = new char*[1000];
						int i,k=0;
						for (i=0; i<1000; i++)
						{
							buf[i]=new char[1000];
							memset(buf[i],' ',1000*sizeof(char));
						}
						
						i=0;
						k=0;
						NODE * jump= root[curr_heap];
						if (jump)
						{
							
							
							showv2(jump,&k,0,buf);
							work(buf,k);
							SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
							for (i=0; i<=k; i++)
							{
								//sprintf(buf[i],"Adancime %d: %s",i,buf[i]);
								SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf[i]);
							}
						}


					
					}
					else 
					{
						MessageBox(hwnd, "Numar Invalid !", "Warning", MB_OK);
					}

				}
				break;
				case IDC_DELMIN:
					{
						NODE* aux=root[curr_heap];
						EXTRACT_MIN(aux);
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);

						char**buf = new char*[1000];
						int i,k=0;
						for (i=0; i<1000; i++)
						{
							buf[i]=new char[1000];
							memset(buf[i],' ',1000*sizeof(char));
						}
						
						i=0;
						k=0;
						NODE * jump= root[curr_heap];
						if (jump)
						{
							
							
							showv2(jump,&k,0,buf);
							work(buf,k);
							SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
							for (i=0; i<=k; i++)
							{
								//sprintf(buf[i],"Adancime %d: %s",i,buf[i]);
								SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf[i]);
							}
						}


					}
					break;
				case IDC_DEGKEY:
					{
						BOOL bSuccess;
						int value1 = GetDlgItemInt(hwnd, IDC_TEXT3, &bSuccess, FALSE);
						if (!bSuccess)
						{
							MessageBox(hwnd, "Numar Invalid !", "Warning", MB_OK);
							break;
						}
						int value2 = GetDlgItemInt(hwnd, IDC_TEXT4, &bSuccess, FALSE);
						if (!bSuccess)
						{
							MessageBox(hwnd, "Numar Invalid !", "Warning", MB_OK);
							break;
						}
						DECREASE_KEY(root[curr_heap],value1,value2);
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);

						
						char**buf = new char*[1000];
						int i,k=0;
						for (i=0; i<1000; i++)
						{
							buf[i]=new char[1000];
							memset(buf[i],' ',1000*sizeof(char));
						}
						
						i=0;
						k=0;
						NODE * jump= root[curr_heap];
						if (jump)
						{
							
							
							showv2(jump,&k,0,buf);
							work(buf,k);
							SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
							for (i=0; i<=k; i++)
							{
								//sprintf(buf[i],"Adancime %d: %s",i,buf[i]);
								SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf[i]);
							}
						}

					}
					break;
				case IDC_FIND:
					{
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
						char *buf=new char[10];
						NODE *aux=root[curr_heap];
						if (MIN_BHEAP(aux))
						sprintf(buf,"Nodul minim are valoarea: %d",MIN_BHEAP(aux)->info);
						else
							sprintf(buf,"Eroare");
						SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);

					}
					break;

				case IDC_UNION:
					{
						BOOL bSuccess;
						int value = GetDlgItemInt(hwnd, IDC_TEXT5, &bSuccess, FALSE);
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);

						MERGE_BHEAP(root[curr_heap],root[value]);
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
						char**buf = new char*[1000];
						int i,k=0;
						for (i=0; i<1000; i++)
						{
							buf[i]=new char[1000];
							memset(buf[i],' ',1000*sizeof(char));
						}
						
						i=0;
						k=0;
						NODE * jump= root[curr_heap];
						if (jump)
						{
							
							
							showv2(jump,&k,0,buf);
							work(buf,k);
							SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
							for (i=0; i<=k; i++)
							{
								//sprintf(buf[i],"Adancime %d: %s",i,buf[i]);
								SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf[i]);
							}
						}

					}
					break;
				case IDC_DEL:
					{
						
						for (int i=curr_heap; i<nr_root; i++)
							root[i]=root[i+1];
						nr_root--;
						curr_heap=0;
						SetDlgItemInt(hwnd, IDC_SHOWCOUNT2 , curr_heap,FALSE);
					SetDlgItemInt(hwnd, IDC_SHOWCOUNT , nr_root,FALSE);
					}
					break;
				case IDC_SELECT:
					{
						BOOL bSuccess;
						curr_heap = GetDlgItemInt(hwnd, IDC_TEXT2, &bSuccess, FALSE);
						if (!bSuccess)
						{
							MessageBox(hwnd, "Numar Invalid !", "Warning", MB_OK);
							break;
						}
						if (!bSuccess)
						{
							MessageBox(hwnd, "Numar Invalid !", "Warning", MB_OK);
							break;
						}
						SetDlgItemInt(hwnd, IDC_SHOWCOUNT2 , curr_heap,FALSE);
						SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
						char**buf = new char*[1000];
						int i,k=0;
						for (i=0; i<1000; i++)
						{
							buf[i]=new char[1000];
							memset(buf[i],' ',1000*sizeof(char));
						}
						
						i=0;
						k=0;
						NODE * jump= root[curr_heap];
						if (jump)
						{
							
							
							showv2(jump,&k,0,buf);
							work(buf,k);
							SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
							for (i=0; i<=k; i++)
							{
								//sprintf(buf[i],"Adancime %d: %s",i,buf[i]);
								SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf[i]);
							}
						}
						
					}
					break;

				case IDC_NEW:
				root[nr_root] = CREATE_BHEAP();
				SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
					curr_heap=nr_root;
					nr_root++;
					SetDlgItemInt(hwnd, IDC_SHOWCOUNT2 , curr_heap,FALSE);
					SetDlgItemInt(hwnd, IDC_SHOWCOUNT , nr_root,FALSE);
					break;
				
					
				
			}
		break;
		
		case WM_CLOSE:
			EndDialog(hwnd, 0);
		break;
		default:
			return FALSE;
	}
	return TRUE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
